/**
 * 
 */
package com.softme.trade.control;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import com.softme.trade.model.Currency;
import com.softme.trade.model.dao.CurrencyDAO;
import com.softme.trade.util.BeanFactory;

/**
 * @author meyer
 */
@ViewScoped
@ManagedBean
public class CurrencyController implements  Serializable
{

	private static final long serialVersionUID = 1L;

	private Currency currency = new Currency();


	private List<Currency> listCurrencies = new ArrayList<Currency>();

	private CurrencyDAO CurrencyDAO;

	public CurrencyController()
	{
		this.updateScreen();
		// TODO Auto-generated constructor stub
	}

	
	/**
	 * Limpa os campos input e atualiza a lista de empresas cadastrados
	 */
	private void updateScreen()
	{
		this.currency = new Currency();
		this.listCurrencies = this.getDAO().loadAll();
	}


	/**
	 * Grava novo registro ou atualiza um registro
	 */
	public void save()
	{
		this.getDAO().saveOrUpdate(getCurrency());
		updateScreen();
	}



	public void delete()
	{
		this.getDAO().delete(this.getCurrency());
		this.updateScreen();
	}

	public void create()
	{
		this.currency = new Currency();
	}

	public Currency getCurrency()
	{
		return this.currency;
	}
	

	public void setCurrency(Currency currency)
	{
		this.currency = currency;
	}

	public CurrencyDAO getDAO()
	{
		if (this.CurrencyDAO == null || this.CurrencyDAO.getHibernateTemplate() == null)
			this.CurrencyDAO = (CurrencyDAO) BeanFactory.getBean("currencyDAO", CurrencyDAO.class);

		return this.CurrencyDAO;
	}

	public List<Currency> getListCurrencies()
	{
		return this.listCurrencies;
	}

}
